package com.dgut.doctorservice.controller;


import com.dgut.commonutils.Result;
import com.dgut.doctorservice.entity.NewsInfo;
import com.dgut.doctorservice.service.NewsInfoService;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;

/**
 * <p>
 *  前端控制器
 * </p>
 *
 * @author dgut
 * @since 2021-09-29
 */
@RestController
@RequestMapping("/doctorservice/news-info")
@CrossOrigin
public class NewsInfoController {

    @Autowired
    NewsInfoService newsInfoService;

    @ApiOperation("添加新闻")
    @PostMapping("addNews")
    public Result addNews(@RequestBody NewsInfo newsInfo){
        boolean save = newsInfoService.save(newsInfo);
        if (save){
            return Result.succ().message("添加新闻成功");
        }else {
            return Result.succ().message("添加新闻失败");
        }
    }

    @ApiOperation("删除新闻")
    @DeleteMapping("deleteNews")
    public Result deleteNews(@RequestParam String id){
        boolean b = newsInfoService.removeById(id);
        if (b){
            return Result.succ().message("删除成功");
        }else {
            return Result.succ().message("删除失败");
        }
    }

    @ApiOperation("修改新闻")
    @PostMapping("updateNews")
    public Result updateNews(@RequestBody NewsInfo newsInfo){
        boolean b = newsInfoService.updateById(newsInfo);
        if (b){
            return Result.succ().message("修改成功");
        }else {
            return Result.succ().message("修改失败");
        }
    }

    @ApiOperation("获取所有新闻")
    @PostMapping("getAll")
    public Result getAll(){
        List<NewsInfo> list = newsInfoService.list();
        if (list.size()==0){
            return Result.succ().message("暂无新闻条目");
        }else {
            return Result.succ().data("list",list);
        }
    }

}

